package net.service.Impl;


import net.mapper.AdminMapper;
import net.pojo.Artile;
import net.pojo.Comments;
import net.pojo.Dicuss;
import net.pojo.Links;
import net.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<Artile> findAll() {
        return adminMapper.findAll();
    }

    @Override
    public List<Artile> search(String ptags, String ptitle) {
        return adminMapper.search(ptags,ptitle);
    }

    @Override
    public int deleteById(Integer pid) {
        return adminMapper.deleteById(pid);
    }

    @Override
    public Artile findById(Integer pid) {
        return adminMapper.findById(pid);
    }

    @Override
    public int updateArtile(Artile artile,@RequestParam(value = "uploadFile",required = false)MultipartFile uploadFile, HttpServletRequest request) {
        if (uploadFile == null){
            artile.setImg(artile.getImg());
            artile.setTime(new Date());
            return adminMapper.updateArtile(artile);
        }else {
            //得到项目的绝对路径
            String path = request.getServletContext().getRealPath("/client/images/travel/");
            //将要上传的图片写入到指定的文件
            if (uploadFile != null){
                //获得上传文件的名称
                String fileName = uploadFile.getOriginalFilename();
                //获取上传文件的后缀
                String suffix = fileName.substring(fileName.lastIndexOf("."));
                //重新命名
                String tempFileName = UUID.randomUUID().toString()+suffix;
                File fileTemp=new File(path);
                if (!fileTemp.exists()){
                    fileTemp.mkdirs();
                }
                File file = new File(path + "\\" + tempFileName);
                try {
                    uploadFile.transferTo(file);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                artile.setImg("/client/images/travel/"+tempFileName);
            }
            artile.setTime(new Date());
            return adminMapper.updateArtile(artile);
        }
    }

    @Override
    public int addArtile(Artile artile, @RequestParam(value = "uploadFile",required = false) MultipartFile uploadFile, HttpServletRequest request) {
        //得到项目的绝对路径
        String path = request.getServletContext().getRealPath("/client/images/travel/");
        //将要上传的图片写入到指定的文件
        if (uploadFile != null){
            //获得上传文件的名称
            String fileName = uploadFile.getOriginalFilename();
            //获取上传文件的后缀
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            //重新命名
            String tempFileName = UUID.randomUUID().toString()+suffix;
            File fileTemp=new File(path);
            if (!fileTemp.exists()){
                fileTemp.mkdirs();
            }
            File file = new File(path + "\\" + tempFileName);

            try {
                uploadFile.transferTo(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
            artile.setImg("/client/images/travel/"+tempFileName);
        }
        artile.setTime(new Date());
        return adminMapper.addArtile(artile);
    }

    @Override
    public List<Dicuss> findAllDicuss() {
        return adminMapper.findAllDicuss();
    }

    @Override
    public int deleteDicuss(Integer did) {
        return adminMapper.deleteDicuss(did);
    }

    @Override
    public List<Comments> findAllComment() {
        return adminMapper.findAllComment();
    }

    @Override
    public int deleteComments(Integer cid) {
        return adminMapper.deleteComments(cid);
    }

    @Override
    public List<Links> findLinks() {
        return adminMapper.findLinks();
    }

    @Override
    public int deleteLinks(Integer lid) {
        return adminMapper.deleteLinks(lid);
    }

    @Override
    public int updateLinks(Links links) {
        return adminMapper.updateLinks(links);
    }

    @Override
    public Links findLinkById(Integer lid) {
        return adminMapper.findLinkById(lid);
    }

    @Override
    public List<Artile> searchNum() {
        return adminMapper.searchNum();
    }
}
